package com.xxxx.server.utils;

import cn.hutool.core.util.StrUtil;
import com.xxxx.server.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: yeb
 * @description: 工具类，用于存放各种可以公共使用的方法，或者处理大量数据的代码
 * @author: xzl
 * @create: 2021-11-16 11:37
 **/
@Component
public class AnyUtil {
    @Autowired
    private RedisTemplate redisTemplate;
    private static AnyUtil anyUtil;

    @PostConstruct
    public void init(){
        anyUtil = this;
        anyUtil.redisTemplate = this.redisTemplate;
    }

    public static String getRandom(List<String> allUserName) {
        int num = 10;
        Random random = new Random();
        String userName = String.valueOf(random.nextInt(89999999) + 10000000);
//        String userName = "123334445555";
        // 使用正则表达式，判断并查出一个字符串中是否包含多个相同的字符的“好账号”
        Pattern pattern = Pattern.compile("(\\w)\\1{3,}");
        Matcher matcher = pattern.matcher(userName);
        while (matcher.find()) {
            // 打印连续出现三次的字符~
            System.out.println(matcher.group());
            return getRandom(allUserName);
        }
//        if (!matcher.find())
//            return getRandom(allUserName);
        boolean contains = allUserName.contains(userName);
        if (contains) return getRandom(allUserName);
        return userName;
    }

    /**
     * 保证menu redis与数据库数据一致
     * 为了对menu做crud时，其他的redis数据无法更新，所以清空所有menu的菜单
     * @return
     */
    public static void updRedisMenu(){
        Set keys = anyUtil.redisTemplate.keys("menu*");
        for (Object key : keys) {
            anyUtil.redisTemplate.opsForValue().getOperations().delete(key);
        }
    }
}
